import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)


const store = new Vuex.Store({
	state: {
		logoInfo: null, // logo信息
		userInfo: null, // 用户信息
		company: null, //公司信息
		userSig: '', // IM密钥
		isManager: false, // 是否是管理人员
	},
	mutations: {
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo;
			if (userInfo) {
				state.isManager = userInfo?.userCompany?.purviewDept?.length > 0 ? true : false
			}
		},

		// 设置IM密钥
		setUserSig(state, userSig) {
			state.userSig = userSig
		},


	},
	actions: {
		// 正常获取用户信息-绑定跟进人
		async getUserInfo({
			state,
			commit,
			dispatch
		}, params = {}) {
			return new Promise(async (resolve, reject) => {
				try {
					let resp = await getUserInfoApi();
					commit("setUserInfo", resp.data.user);
					if (state.userInfo.role !== 'intention_owner') {
						// 获取IM密钥
						dispatch("getOnlyId");
					}
					resolve()
				} catch (e) {
					console.log(`%c正常获取用户信息-绑定跟进人${e.message}`, 'background:orange');
					reject(e)
				}
			})

		},
	}
})
export default store